package com.irs.irsforum.web;


import com.irs.irsforum.security.access.Access;
import com.irs.irsforum.security.access.UserType;
import com.irs.irsforum.service.IBehaviorService;
import com.irs.irsforum.service.IFavoriteService;
import com.irs.irsforum.service.IPostService;
import com.irs.irsforum.web.common.BaseController;
import com.irs.irsforum.web.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yihuili
 * @since 2020-01-18
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController extends BaseController {
    private static final Logger logger =
            LoggerFactory.getLogger(FavoriteController.class);

    @Autowired
    IFavoriteService iFavoriteService;

    @Autowired
    IBehaviorService iBehaviorService;

    @Autowired
    IPostService iPostService;

    /**
     * 收藏，并记录行为behaviorType=0
     *
     * @param postId 帖子Id
     * @param userId 用户Id
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
        @RequestMapping(value = "/setfavorite", method = RequestMethod.POST)
    public Object setFavorite(@RequestParam(value = "postId") Long postId,
                              @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId) {
        logger.info("userId={}的用户，收藏post={}的帖子", userId, postId);
        Integer favoriteResult = 0;
        try {
            favoriteResult = iFavoriteService.setFavorite(postId, userId);
            if (favoriteResult == 1) {
                iBehaviorService.setBehaviorType(postId, userId, 0);
                iPostService.updateFavoriteNum(postId, 1L);
                return ResponseResult.newSuccessResult("收藏成功");
            } else {
                logger.info("userId={}的用户，收藏post={}的帖子失败,favoriteResult ={}", userId, postId, favoriteResult);
                return ResponseResult.newFailResult("已收藏或收藏失败");
            }
        } catch (SQLException e) {
            logger.info("userId={}的用户，收藏post={}的帖子失败,favoriteResult ={}", userId, postId, favoriteResult);
            return ResponseResult.newFailResult("已收藏或收藏失败");
        }
    }

    /**
     * 取消收藏，并删除行为behaviorType
     *
     * @param postId 帖子Id
     * @param userId 用户Id
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/cancelfavorite", method = RequestMethod.DELETE)
    public Object cancelFavorite(@RequestParam(value = "postId") Long postId,
                                 @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId) {
        logger.info("取消收藏postId={},userId={}", postId, userId);
        Integer result = iFavoriteService.cancelFavorite(postId, userId);
        if (result == 1) {
            iBehaviorService.updateBehavior(postId, userId, 1);
            iPostService.updateFavoriteNum(postId, -1L);
            return ResponseResult.newSuccessResult();
        } else {
            logger.error("取消收藏postId={},userId={}失败", postId, userId);
            return ResponseResult.newFailResult("取消失败");
        }
    }
}

